package com.wing.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.purchase.dao.SupplierDueSubjectCategoryDao;
import com.wing.purchase.model.entity.SupplierCategory;
import com.wing.purchase.model.entity.SupplierDueSubjectCategory;
import com.wing.purchase.model.request.SupplierDueSubjectCategoryForm;
import com.wing.purchase.model.response.NodeDataVO;
import com.wing.purchase.model.response.SupplierCategoryVO;
import com.wing.purchase.model.response.SupplierDueSubjectCategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 渠道尽调科目分类-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class SupplierDueSubjectCategoryServiceImpl extends ServiceImpl<SupplierDueSubjectCategoryDao, SupplierDueSubjectCategory> implements SupplierDueSubjectCategoryService {

    private long ROOT_ID=-1l;

    @Override
    public int add(SupplierDueSubjectCategoryForm supplierDueSubjectCategoryForm) {
        if(supplierDueSubjectCategoryForm!=null&&supplierDueSubjectCategoryForm.getIsFinal()==null){
            supplierDueSubjectCategoryForm.setIsFinal(false);
        }
        //校验

        SupplierDueSubjectCategory supplierDueSubjectCategory =new SupplierDueSubjectCategory();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(SupplierDueSubjectCategory.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(supplierDueSubjectCategoryForm, supplierDueSubjectCategory, copyOptions);

        return baseMapper.insert(supplierDueSubjectCategory);
    }

    @Override
    public List<SupplierDueSubjectCategoryVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<SupplierDueSubjectCategoryVO> pageList4app(Page page, Map<String, Object> params) {
        List<SupplierDueSubjectCategoryVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public SupplierDueSubjectCategoryVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public JsonResult getTreeData(Long selectId) {

        List<SupplierDueSubjectCategoryVO> list=baseMapper.list4app(new HashMap<>());
        Map<Long,List<NodeDataVO>> map=new HashMap<>();
        for (SupplierDueSubjectCategoryVO supplierDueSubjectCategoryVO:list) {
            if (map.containsKey(supplierDueSubjectCategoryVO.getParentId())){
                NodeDataVO nodeDataVO=new NodeDataVO();
                nodeDataVO.setValue(supplierDueSubjectCategoryVO.getId().toString());
                nodeDataVO.setName(supplierDueSubjectCategoryVO.getName());
                if(!supplierDueSubjectCategoryVO.getIsFinal() || !supplierDueSubjectCategoryVO.getIsDeleted()){
                    nodeDataVO.setDisabled(true);
                }
                if(selectId != null && selectId.equals(supplierDueSubjectCategoryVO.getId())){
                    nodeDataVO.setSelected(true);
                }
                map.get(supplierDueSubjectCategoryVO.getParentId()).add(nodeDataVO);
            }else{
                List<NodeDataVO> tmpList=new ArrayList<>();
                NodeDataVO nodeDataVO=new NodeDataVO();
                nodeDataVO.setValue(supplierDueSubjectCategoryVO.getId().toString());
                nodeDataVO.setName(supplierDueSubjectCategoryVO.getName());
                if(!supplierDueSubjectCategoryVO.getIsFinal() || !supplierDueSubjectCategoryVO.getIsDeleted()){
                    nodeDataVO.setDisabled(true);
                }
                if(selectId != null && selectId.equals(supplierDueSubjectCategoryVO.getId())){
                    nodeDataVO.setSelected(true);
                }
                tmpList.add(nodeDataVO);
                map.put(supplierDueSubjectCategoryVO.getParentId(),tmpList);
            }
        }
        List<NodeDataVO> rootList=map.get(-1L);
        if (rootList==null){
            return JsonResult.success();
        }
        constructTree(rootList,map);
        return JsonResult.success(rootList);
    }

    @Override
    public boolean delete(Long supplierDueSubjectCategoryId) {
        // 查询当前删除分类是否存在子类
        int count = baseMapper.selectCount(new QueryWrapper<SupplierDueSubjectCategory>().eq("parent_id", supplierDueSubjectCategoryId));
        if(count > 0){
            throw new BusinessException(500, "请先删除子分类");
        }
        return removeById(supplierDueSubjectCategoryId);
    }

    @Override
    public int enable(Long id) {
        SupplierDueSubjectCategoryVO supplierDueSubjectCategoryVO = baseMapper.get4appById(id);
        if (supplierDueSubjectCategoryVO!=null){
            SupplierDueSubjectCategory supplierDueSubjectCategory = new SupplierDueSubjectCategory();
            supplierDueSubjectCategory.setId(id);
            supplierDueSubjectCategory.setIsDeleted(true);
            return baseMapper.updateById(supplierDueSubjectCategory);
        }
        return 0;
    }

    @Override
    public int disable(Long id) {
        SupplierDueSubjectCategoryVO supplierDueSubjectCategoryVO = baseMapper.get4appById(id);
        if (supplierDueSubjectCategoryVO!=null){
            SupplierDueSubjectCategory supplierDueSubjectCategory = new SupplierDueSubjectCategory();
            supplierDueSubjectCategory.setId(id);
            supplierDueSubjectCategory.setIsDeleted(false);
            return baseMapper.updateById(supplierDueSubjectCategory);
        }
        return 0;
    }

    private void  constructTree(List<NodeDataVO> parentList,Map<Long,List<NodeDataVO>> dataMap){
        for (NodeDataVO node:parentList) {
            Long nodeId=Long.valueOf(node.getValue());
            List<NodeDataVO> childList=dataMap.get(nodeId);
            if (childList!=null){
                node.setChildren(childList);
                constructTree(childList,dataMap);
            }
        }
    }
}
